/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: TokenAuthenticationFilter
 * @Package com.rx.core.filter
 * @Description: 上下文获取token
 * @author: 陈锦韬
 * @date: 2021\6\24 0024
 * @version V1.0
 * @Copyright: 2021 陈锦韬  All rights reserved.
 */
package com.rx.core.filter;

import com.alibaba.fastjson.JSON;
import com.rx.core.base.RxBaseConstant;
import com.rx.core.base.RxResponse;
import com.rx.core.controller.OAuthController;
import com.rx.core.enm.RESULT_CODE;
import com.rx.core.enm.RX_LOG_MODE;
import com.rx.core.util.EmptyChecker;
import com.rx.core.util.RxUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author: Administrator
 * @Description: 上下文获取token
 * @date: 2021\6\24 0024
 */
@Component
@Slf4j
@Order(0)
public class RxSecretFilter extends OncePerRequestFilter {
    //
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

        if(request.getRequestURI().contains(OAuthController.RSA_SECRET)){
            log.info(RX_LOG_MODE.DEFAULT.getFormat(),"获取秘钥");
            String token = request.getHeader(RxBaseConstant.HEARER_LIMIT_MARK);
            if (EmptyChecker.isEmpty(token) || !RxBaseConstant.SECRET_LIMIT_MARK.equalsIgnoreCase(token)){
                RxResponse.response(response,"本接口为指定客户访问");
                return;
            }
        }

        if (EmptyChecker.notEmpty(filterChain)){
            filterChain.doFilter(request,response);
        }
    }
}
